package ldkxingzhe.ldkxingzhedemo.view;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;

import ldkxingzhe.ldkxingzhedemo.utils.LogDemo;

/**
 * Created by Administrator on 2016/3/4.
 */
public class CircleDrawable extends Drawable{
    @SuppressWarnings("unused")
    private static final String TAG = "CircleDrawable";

    private final Paint paint;
    private float radius;
    private final RectF mRect = new RectF();
    private final RectF mBitmapRect;
    private final BitmapShader bitmapShader;
    private final Paint strokePaint;
    private final float strokeWidth;
    private float strokeRadius;

    public CircleDrawable(Bitmap bitmap, Integer strokeColor, float strokeWidth){
        radius = Math.min(bitmap.getWidth(), bitmap.getHeight())/2;

        bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        mBitmapRect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
        LogDemo.v(TAG, "mBitmapRect is " + mBitmapRect);

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(bitmapShader);
        paint.setFilterBitmap(true);
        paint.setDither(true);
        this.strokeWidth = strokeWidth;
        if(strokeColor == null){
            strokePaint = null;
        }else{
            strokePaint = new Paint();
            strokePaint.setStyle(Paint.Style.STROKE);
            strokePaint.setColor(strokeColor);
            strokePaint.setStrokeWidth(strokeWidth);
        }
        strokeRadius = radius - strokeWidth/2;
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        mRect.set(0, 0, bounds.width(), bounds.height());
        radius = Math.min(bounds.width(), bounds.height())/2;
        strokeRadius = radius - strokeWidth / 2;

        Matrix shaderMatrix = new Matrix();
        shaderMatrix.setRectToRect(mBitmapRect, mRect, Matrix.ScaleToFit.FILL);
        bitmapShader.setLocalMatrix(shaderMatrix);
        LogDemo.v(TAG, "onBoundsChanged, and bounds is " + bounds);
    }

    @Override
    public int getIntrinsicWidth() {
        return (int) mRect.width();
    }


    @Override
    public int getIntrinsicHeight() {
        return (int) mRect.height();
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.drawCircle(radius, radius, radius, paint);
        if(strokePaint != null){
            canvas.drawCircle(radius, radius, radius + strokeRadius, strokePaint);
        }
    }

    @Override
    public void setAlpha(int alpha) {
        paint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        paint.setColorFilter(cf);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.OPAQUE;
    }
}
